package fr.cie.blog.modeldao.hib;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import fr.cie.blog.beans.User;
import fr.cie.blog.domainservice.IServiceUser;

/**
 * @author AetM
 * 
 */
public class UserDaoImplHib extends HibernateDaoSupport implements
		IServiceUser {
	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger.getLogger(UserDaoImplHib.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#update(java.lang.Object)
	 */
	@Override
	public final int save(final User user) {
		// Doesn't work
		// getHibernateTemplate().getSessionFactory().getCurrentSession();
		// getHibernateTemplate().saveOrUpdate(user);

		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(user);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update User error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#update(java.lang.Object)
	 */
	@Override
	public final int update(final User user) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(user);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update User error with user.login=" + user.getLogin(),
					e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#delete(java.lang.Object)
	 */
	@Override
	public final int delete(final User user) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.delete(user);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update User error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findAll()
	 */
	@Override
	public final List<User> findAll() {
		@SuppressWarnings("unchecked")
		final List<User> list = (List<User>) getHibernateTemplate().find(
				"from User");
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findById(int)
	 */
	@Override
	public final User findById(final long pIdentifier) {
		return getHibernateTemplate().get(User.class, pIdentifier);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceUser#findByLogin(int)
	 */
	@Override
	public final User findByLogin(final String pLogin) {
		User user = null;

		@SuppressWarnings("unchecked")
		final List<User> userList = (List<User>) getHibernateTemplate().find(
				"from User as u where u.login = ?", pLogin);

		if (!(userList == null || userList.isEmpty())) {
			user = (User) userList.get(0);
		}
		return user;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.cie.blog.domainservice.IServiceUser#authenticateUser(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public final User authenticateUser(final String login,
			final String password) {
		User user = null;

		@SuppressWarnings("unchecked")
		final List<User> userList = (List<User>) getHibernateTemplate()
				.findByNamedParam(
						"from User as u where u.login = :login "
								+ "and u.password = :password",
						new String[] { "login", "password" },
						new Object[] { login, password });

		if (!(userList == null || userList.isEmpty())) {
			user = (User) userList.get(0);
		}

		return user;
	}
}